/**
 * Created by xxcao on 2017/6/5.
 */
var url = require('url');
var uuid = require('uuid');
var crypto = require('crypto');

module.exports = {
    removeFavicon: function (req, res) {
        var pathname = url.parse(req.url).pathname;
        if(pathname === '/favicon.ico'){
            return false;
        }
        return true;
    },
    toJson:function (code = 0, message = '', data) {
        var res = null;
        if (data) {
            res = { response : JSON.stringify({code, message, data}) };
        } else {
            res = { response : JSON.stringify({code, message}) };
        }
        var date = new Date();
        console.log(['TIME:' + date.toTimeString() + " " + date.toDateString(),'RESPONSE:' + JSON.stringify(res)]);
        return res;
    },
    isParameterEmpty:function () {
        for(var i = 0; i < arguments.length; i++){
            if (arguments[i] === undefined ||
                arguments[i] === null ||
                arguments[i] === ''){
                return true;
            }
        }
        return false;
    },
    isValidSession:function (req, method) {
        if (method === 'GET' || method === 'get') {
            if (req.session.user === null || req.session.user !== req.query.user_id) {
                return false;
            }
        } else {
            if (req.session.user === null|| req.session.user !== req.body.user_id) {
                return false;
            }
        }
        return true;
    },
    pageSize:function () {
        return 20;
    },
    getUUID:function () {
        var uuidStr = uuid.v1();
        return uuidStr.replace(/-/g,'');
    },
    logInfo:function (type, name, method, data) {
        var date = new Date();
        if (type === 0){
            //request
            console.log(['TIME:' + date.toTimeString() + " " + date.toDateString(), 'NAME:' + name, 'METHOD:' + method, 'REQUEST:' + JSON.stringify(data)]);
        } else {
            //response
            console.log(['TIME:' + date.toTimeString() + " " + date.toDateString(), 'NAME:' + name, 'METHOD:' + method, 'RESPONSE:' + JSON.stringify(data)]);
        }
    },
    getMD5String:function (string) {
        var hexDigits = ['0', 'n', '1', 'j', '2', 'a', '3', 'q', '4', 's', '5', 'w', '6', 'c', '7', 'e'];
        var md5sum = crypto.createHash('md5');
        md5sum.update(string);
        var bytes = md5sum.digest();
        var schar = [];
        var k = 0;
        for(var i = 0; i < bytes.length; ++i) {
            var byte0 = bytes[i];
            schar[k++] = hexDigits[byte0 >>> 4 & 15];
            schar[k++] = hexDigits[byte0 & 15];
        }
        var res = schar.join('');
        return res;
    },
    getMonthDays: function getDadys(whichYear, whichMonth) {
       var nextMoth = whichMonth + 1;
       var nextYear = whichYear;
       if(nextMoth == 13)  {
        nextMoth = 1;
        nextYear++;
       }
       var theCurrentDate = whichYear + "-" + whichMonth + "-1";
       var theNextDate = nextYear + "-" + nextMoth + "-1";
       var yearObjOne = new Date(theCurrentDate);
       var yearObjTwo = new Date(theNextDate);

       var milliseconds = yearObjTwo.getTime() - yearObjOne.getTime();
       var daymilliseconds = 3600 * 24 * 1000;
       return (milliseconds/daymilliseconds);
    },
    getRandomString:function (len) {
            len = len || 32;
            var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
            var maxPos = $chars.length;
            var pwd = '';
            for (var i = 0; i < len; i++) {
                pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
            }
            return pwd;
        }
};